/* 
 * File:   Menu.cpp
 * Author: agu
 * 
 * Created on 16 de junio de 2014, 17:13
 */

#include "client_Menu.h"
#include <iostream>

Menu::Menu(Glib::RefPtr<Gtk::Application> aplicacion) {
    this->app = aplicacion;
    //Load the GtkBuilder file and instantiate its widgets:
    this->builder = Gtk::Builder::create();
    try {
        this->builder->add_from_file("gladeInterfaces/menu.glade");
    } catch (const Glib::FileError& ex) {
        std::cout << "FileError: " << ex.what() << std::endl;

    } catch (const Glib::MarkupError& ex) {
        std::cout << "MarkupError: " << ex.what() << std::endl;

    } catch (const Gtk::BuilderError& ex) {
        std::cout << "BuilderError: " << ex.what() << std::endl;
    }
}

void Menu::loadWidgets() {
    this->builder->get_widget("window", this->window);
    this->builder->get_widget("buttonNew", this->buttonNew);
    this->builder->get_widget("buttonQuit", this->buttonQuit);

}

void Menu::connectWidgets() {
    this->window->signal_hide().connect(sigc::mem_fun(this, &Menu::close));
    this->buttonQuit->signal_clicked().connect(sigc::mem_fun(this, &Menu::close));
    this->buttonNew->signal_clicked().connect(sigc::mem_fun(this, &Menu::newGame));
}

Menu::~Menu() {
    if (this->window != NULL)
    delete this->window;
}

void Menu::close() {
    std::cout << "Menu cerrar" << std::endl;    
    this->window->close();
}

void Menu::newGame() {
    this->newMenuGame.setSocket(this->socket);
    this->newMenuGame.setWindowsBack(this->window);
    this->buttonNew->set_visible(false);
    this->newMenuGame.run();
}

void Menu::options() {    
}

void Menu::run() {
    this->loadWidgets();
    this->connectWidgets();
    app->run(*this->window);
}

void Menu::setSocket(Client_socket s) {
    this->socket = s;
}


